//
//  SEGSoundModel.m
//  ExcellentHome
//
//  Created by 刘山 on 2017/11/20.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGSoundModel.h"
#define kSEGSoundDataPath [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"soundData.data"]

@implementation SEGSoundModel
+(NSArray<NSArray<SEGSoundModel*>*>*)seg_initData{
    SEGSoundModel *t1 = [SEGSoundModel new];
    t1.name = @"开门提示音";
    t1.kitType = @"3";
    t1.soundName = @"";
    t1.activity = @"1";
//    NSString *string = [[NSUserDefaults standardUserDefaults] valueForKey:kPlayOpenDoorSoundKey];
//    t1.activity = (string && [string isEqualToString:@"0"]) ? @"0" : @"1";
    
    SEGSoundModel *t2 = [SEGSoundModel new];
    t2.name = @"可爱女童";
    t2.kitType = @"4";
    t2.soundName = @"a1";
    t2.activity = @"1";
    
    SEGSoundModel *t3 = [SEGSoundModel new];
    t3.name = @"清朗男声";
    t3.kitType = @"4";
    t3.soundName = @"a2";
    t3.activity = @"0";
    
    SEGSoundModel *t4 = [SEGSoundModel new];
    t4.name = @"悠扬女声";
    t4.kitType = @"4";
    t4.soundName = @"a4";
    t4.activity = @"0";
    
    SEGSoundModel *t5 = [SEGSoundModel new];
    t5.name = @"稚趣男童";
    t5.kitType = @"4";
    t5.soundName = @"a3";
    t5.activity = @"0";

    [self seg_saveSoundDataWithData:@[@[t1],@[t2,t3,t4,t5]]];
    return @[@[t1],@[t2,t3,t4,t5]];
}


- (void)encodeWithCoder:(NSCoder *)encoder
{
    unsigned int count = 0;
    objc_property_t *propertes = class_copyPropertyList([self class], &count);
    for (int i=0; i<count; i++) {
        const char *propertyName = property_getName(propertes[i]);
        NSString *name = [NSString stringWithUTF8String:propertyName];
        id value = [self valueForKey:name];
        [encoder encodeObject:(value)?value:@"" forKey:name];
    }
}

- (id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super init]) {
        unsigned int count =0;
        objc_property_t *propertes = class_copyPropertyList([self class], &count);
        for (int i = 0; i < count; i++) {
            const char *propertyName = property_getName(propertes[i]);
            NSString *name = [NSString stringWithUTF8String:propertyName];
            id value = [decoder decodeObjectForKey:name];
            [self setValue:(value)?value:@"" forKey:name];
        }
        
    }
    return self;
}

+(void)seg_saveSoundDataWithData:(NSArray<NSArray<SEGSoundModel*>*>*)data{
    [NSKeyedArchiver archiveRootObject:data toFile:kSEGSoundDataPath];
    
    [data enumerateObjectsUsingBlock:^(NSArray<SEGSoundModel *> * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj enumerateObjectsUsingBlock:^(SEGSoundModel * _Nonnull obj1, NSUInteger idx1, BOOL * _Nonnull stop1) {
            if ([obj1.kitType isEqualToString:@"4"] && [obj1.activity isEqualToString:@"1"]) {
                [[NSUserDefaults standardUserDefaults]setObject:ValueToString(obj1.soundName) forKey:@"SOUNDNAME"];
                *stop = YES;
            }
        }];
    }];
}

+(NSArray<NSArray<SEGSoundModel*>*>*)seg_getSoundData{
    return [NSKeyedUnarchiver unarchiveObjectWithFile:kSEGSoundDataPath];
}
@end

